﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Net.Mail;
using System.Net;

public static class MailHelper
{
    public static void MailAt(string from, string to, string subject, string body)
    {
        string mailServerName = Convert.ToString(ConfigurationManager.AppSettings["mailServerName"]);
        try
        {
            //Eğer credential ile yapılacaksa from ile credentialı yapacak aynı olmalı..
            //MailMessage oMessage = new MailMessage(from, to, subject, body);
            MailMessage oMessage = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailServerUserName"].ToString(), to, subject, body);
            oMessage.IsBodyHtml = true;
            SmtpClient mailClient = new SmtpClient();
            mailClient.Host = mailServerName;
            mailClient.Credentials = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["mailServerUserName"].ToString(), EncryptionHelper.Decrypt(System.Configuration.ConfigurationManager.AppSettings["mailServerUserPassword"].ToString()));
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(oMessage);
        }
        catch (Exception errata)
        {
            throw new Exception(errata.Message);
        }
    }
    public static void MailAt(string to, string subject, string body, MailPriority priority)
    {
        string mailServerName = Convert.ToString(ConfigurationManager.AppSettings["mailServerName"]);
        try
        {
            MailMessage oMessage = new MailMessage(System.Configuration.ConfigurationManager.AppSettings["mailServerUserName"].ToString(), to, subject, body);
            oMessage.IsBodyHtml = true;
            oMessage.Priority = priority;
            SmtpClient mailClient = new SmtpClient();
            mailClient.Host = mailServerName;
            mailClient.Credentials = new NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["mailServerUserName"].ToString(), EncryptionHelper.Decrypt(System.Configuration.ConfigurationManager.AppSettings["mailServerUserPassword"].ToString()));
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(oMessage);
        }
        catch (Exception errata)
        {
            throw new Exception(errata.Message);
        }
    }
    public static void MailAt(string from, string to, string subject, string body, string mailServerUserName, string mailServerUserPassword, MailPriority priority, bool passwordEncrypted)
    {
        string mailServerName = Convert.ToString(ConfigurationManager.AppSettings["mailServerName"]);
        try
        {
            //MailMessage oMessage = new MailMessage(from, to, subject, body);
            MailMessage oMessage = new MailMessage(mailServerUserName, to, subject, body);
            oMessage.IsBodyHtml = true;
            oMessage.Priority = priority;
            SmtpClient mailClient = new SmtpClient();
            mailClient.Host = mailServerName;
            if (passwordEncrypted)
                mailClient.Credentials = new NetworkCredential(mailServerUserName, EncryptionHelper.Decrypt(mailServerUserPassword));
            else
                mailClient.Credentials = new NetworkCredential(mailServerUserName, mailServerUserPassword);
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(oMessage);
        }
        catch (Exception errata)
        {
            throw new Exception(errata.Message);
        }
    }
}
